<?php

namespace app\wxkswich\controller;

use support\Request;
use support\Cache;
use Workerman\Timer;
use Workerman\Worker;
use Workerman\Protocols\Http\Chunk;
use support\Response;
class IndexController
{
    //不需要登录的接口
    protected $noNeedLogin = ['index','GetSWStatus'];

    public function index(Request $request)
    {
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $SwIndex = $request->post('SwIndex','');
        if(!$SwIndex){
            return json(['code'=>0,'msg'=>'缺失参数SwIndex']);
        }
        $SwCtl = $request->post('SwCtl','');
        if(!in_array($SwCtl,[0,1])){
            return json(['code'=>0,'msg'=>'SwIndex仅可为1或0']);
        }
        $mqtt = new \Workerman\Mqtt\Client('mqtt://mqtt.tinycool.cn:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"wxk668899"
        ));
        $mqtt->onConnect = function($mqtt) use ($device_name,$SwIndex,$SwCtl) {
            $mqtt->subscribe('wxk/send');
            $data['cmd'] = 'CtlSw';
            $data['targetDevice'] = $device_name;
            $data['sourceDevice'] = $device_name;
            $data['data'] = ['SwIndex'=>(int)$SwIndex,'SwCtl'=>(int)$SwCtl];
            $str = json_encode($data,320);
            $mqtt->publish('wxk/publish', $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){
            $json = json_decode($content,true);
            $data['code'] = 1;
            $data['msg'] = '设置成功';
            $data['data'] = $json;
            if(isset($json['cmd']) && $json['cmd']=='CtlSw'){
                $http_connection->send(new Chunk(json_encode($data,320)));
                $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
                $mqtt->close();
            }
            
            
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //获取插座状态
    public function GetSWStatus(Request $request){
        $http_connection = $request->connection;

        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $SwIndex = $request->post('SwIndex','');
        if(!$SwIndex){
            return json(['code'=>0,'msg'=>'缺失参数SwIndex']);
        }
        $mqtt = new \Workerman\Mqtt\Client('mqtt://mqtt.tinycool.cn:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"wxk668899"
        ));
        $mqtt->onConnect = function($mqtt) use ($device_name,$SwIndex) {
            $mqtt->subscribe('wxk/send');
            $data['cmd'] = 'GetSWStatus';
            $data['targetDevice'] = $device_name;
            $data['sourceDevice'] = $device_name;
            $data['SwIndex'] = (int)$SwIndex;
            $str = json_encode($data,320);
            $mqtt->publish('wxk/publish', $str);//向主题发送内容

        };

        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){
            $json = json_decode($content,true);
            $data['code'] = 1;
            $data['msg'] = '查询成功';
            $data['data'] = $json;
            if(isset($json['Cmd']) && $json['Cmd']=='pushSwitchStatus'){
                $http_connection->send(new Chunk(json_encode($data,320)));
                $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
                $mqtt->close();
            }
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }

}
